package com.android.ddfafu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.ddfafu.MapApplication;
import com.android.ddfafu.R;
import com.android.ddfafu.domian.Data;
import com.android.ddfafu.domian.ResultInfo;
import com.android.ddfafu.result.UserData;
import com.android.ddfafu.utils.UserHttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class forget_activity extends AppCompatActivity {

    private Button sentEmail;
    private EditText emailEdit;
    private Button return_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        sentEmail = findViewById(R.id.sentbutton);

        sentEmail.setOnClickListener(v -> {
            //发送邮件 http://101.133.141.41/User/user/forget
            String username = emailEdit.getText().toString();
            if (StringUtils.isEmpty(username)){
                //输入为空
                Toast.makeText(forget_activity.this,"请输入邮箱",Toast.LENGTH_SHORT).show();
                return;
            }
            else {
                String pattern = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
                if (!username.matches(pattern)){
                    Toast.makeText(forget_activity.this,"请输入正确的邮箱格式",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            UserHttpUtil.setEmailInForget("http://101.133.141.41/User/user/forget", username, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //发送成功
                }
            });
            //等待用户在邮箱中拿到  临时密码后 点击确认 直接登录
        });
        emailEdit = findViewById(R.id.email_edit);
        return_btn = findViewById(R.id.return_btn);
        return_btn.setOnClickListener(v -> {
            //点击后 直接登录
            Intent intent = new Intent(this,Login_activity.class);
            startActivity(intent);
        });

    }
}
